﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    int MaKH; 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tendn"] == null)
            Response.Redirect("~/dangnhap.aspx");
        if (Session["giohang"] == null)
            Response.Redirect("~/giohang.aspx");
        if (Session["tendn"] != null)
        {
            string s = "Select makh,tenkh,diachi,dienthoai,email from KhachHang where tendn='" + Session["tendn"].ToString() + "'";
            DataTable dt = XLDL.Docbang(s);
            if (dt.Rows.Count != 0)
            {
                MaKH = int.Parse(dt.Rows[0][0].ToString());
                lbHoTen.Text = dt.Rows[0][1].ToString();
                lbDiaChi.Text = dt.Rows[0][2].ToString();
                lbDienThoai.Text = dt.Rows[0][3].ToString();
                lbEmail.Text = dt.Rows[0][4].ToString();
            }
        }
        if (Session["giohang"] != null)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["giohang"];
            System.Int32 tongThanhTien = 0;
            foreach (DataRow r in dt.Rows)
            {
                r["Thanhtien"] = Convert.ToInt32(r["SoLuong"]) * Convert.ToInt32(r["Dongia"]);
                tongThanhTien += Convert.ToInt32(r["Thanhtien"]);
                lbTongtien.Text = tongThanhTien.ToString();
            }
            GVthanhtoan.DataSource = dt;
            GVthanhtoan.DataBind();
        }
        if (!IsPostBack)
        {
            CalNgaygiao.SelectedDate = DateTime.Today;
        }
    }
    protected void btDongy_Click(object sender, EventArgs e)
    {
        //Lưu dữ liệu vào database------------
        int hinhthucthanhtoan, hinhthucgiaohang;
        string Ngaygiao, Ngaydathang, Tennguoinhan, Diachinhan, Dienthoainhan;

        //Gán giá trị cho các biến------------
        Ngaydathang = DateTime.Today.ToString();
        Ngaygiao = CalNgaygiao.SelectedDate.ToString();
        Tennguoinhan = txtNguoinhan.Text;
        Diachinhan = txtDiachinhan.Text;
        Dienthoainhan = txtDienthoainhan.Text;
        Int32 tongThanhTien = Int32.Parse(lbTongtien.Text);
        if (rdThanhtoantruoc.Checked == true)
            hinhthucthanhtoan = 1;
        else
            hinhthucthanhtoan = 0;
        if (rdGiaotructiep.Checked == true)
            hinhthucgiaohang = 1;
        else
            hinhthucgiaohang = 0;

        try
        {
            string s = "INSERT INTO DonHang(makh,ngaydathang,ngaygiaohang,tennguoinhan,diachinhan,dienthoainhan,httt,htgh,trigia) VALUES(" + MaKH + ",'" + NgayDH + "','" + Ngaygiaohang + "',N'" + TenNguoiNhan + "',N'" + DiaChiNhan + "','" + DienThoaiNhan + "'," + hinhthucthanhtoan + "," + hinhthucgiaohang + "," + tongThanhTien + ")";         
            XLDL.thuchienlenh(s);
            //Lay SoDH vua nhap sau cung  
            s = "Select Max(madh) from DonHang Where makh=" + MaKH;
            int MaDH = int.Parse(XLDL.Getdata(s).ToString());
            DataTable dt = new DataTable();
            dt = (DataTable)Session["giohang"];
            int MaSP, Soluong, Dongia, Thanhtien;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                MaSP = int.Parse(dt.Rows[i]["masp"].ToString());
                Soluong = int.Parse(dt.Rows[i]["soluong"].ToString());
                Dongia = int.Parse(dt.Rows[i]["dongia"].ToString());
                Thanhtien = int.Parse(dt.Rows[i]["thanhtien"].ToString());
                s = "INSERT INTO ThanhToan(madh,masp,soluong,dongia,thanhtien) VALUES(" + MaDH + "," + MaSP + "," + Soluong + "," + Dongia + "," + Thanhtien + ")";
                XLDL.thuchienlenh(s);
            }
            Response.Redirect("~/xacnhandonhang.aspx");
        }
        catch
        {
            lbBaoloi.Text = " Xin lỗi <br> Quá trình cập nhật dữ liệu không thành công!";
        }
    }

    public string NgayDH { get; set; }

    public string Ngaygiaohang { get; set; }

    public string TenNguoiNhan { get; set; }

    public string DiaChiNhan { get; set; }

    public string DienThoaiNhan { get; set; }

    public string hinhthucthanhtoan { get; set; }
}